﻿using Assets.Scripts.UI;
using UnityEngine;
using UnityEngine.UI;
public class AnswerDetailItem : UIBase<AnswerDetailItem>
{ 
    public Text AnsText;
    public Image  Background;

    private Sprite upSprite;
    private Sprite downSprite;
    private RectTransform Rect;//gameObject的RectTransform组件

    protected override UIType UIType
    {
        get { return UIType.Item; }
    }

    protected override void InitOnAwake()
    {
        AutoFindComponent(transform, true);
        gameObject.SetActive(false);
        downSprite = Resources.Load<Sprite>("UI/Background/answer_detail_down_bg");
        upSprite = Resources.Load<Sprite>("UI/Background/answer_detail_up_bg");
        Rect = gameObject.GetComponent<RectTransform>();
    }

    /// <summary>
    /// 显示
    /// </summary>
    /// <param name="position"></param>
    /// <param name="answerText"></param>
    /// <param name="ansTextItemHeight"></param>
    public void Show(Vector3 position,string answerText,float ansTextItemHeight)
    {
        AnsText.text = answerText;
        //todo
        this.gameObject.SetActive(true);
        Rect.position = position;
        if (-Rect.anchoredPosition.y + Rect.sizeDelta.y > Screen.height) //超出界面
        {
            position = new Vector2(position.x, position.y + Rect.sizeDelta.y + ansTextItemHeight);
           Rect.position = position;
            Background.sprite = downSprite;
        } else
        {
            Rect.position = position;
            Background.sprite = upSprite;
        }
    }

    /// <summary>
    /// 隐藏
    /// </summary>
    public void Behind()
    {
        this.gameObject.SetActive(false);
    }
}
    
    